/* leveled-config.cxx */

#include "leveled.hxx"
#include "leveled-config.hxx"


unsigned int leveled_config_save_settings(struct leveled_config_struct save_this)
{
	FILE *settFile;
	unsigned int ret = 0;
	char tempChar[255];
	
   	sprintf(tempChar, "%s/leveled.conf", getenv("HOME"));
	
	if ((settFile = fopen(tempChar, "w")) != NULL)
	{
		save_this.id[0] = 'L';
		save_this.id[1] = 'C';
		save_this.id[2] = 'F';
		fwrite(&save_this, sizeof(save_this), 1, settFile);
		fclose(settFile);
		ret = 1;
	}
	return ret;
}

struct leveled_config_struct leveled_config_load_settings(void)
{
    FILE *settFile;
    struct leveled_config_struct return_config;
    char tempChar[255];
    
   	sprintf(tempChar, "%s/leveled.conf", getenv("HOME"));
    
    unsigned int config_invalid = 0;
    if ((settFile = fopen(tempChar, "r")) != NULL)
    {
		fread(&return_config, sizeof(struct leveled_config_struct), 1, settFile);
		if (!((return_config.id[0] == 'L') && (return_config.id[1] == 'C') && (return_config.id[2] == 'F')))
		{
			config_invalid = 1;
		}
		fclose(settFile);
    } else
    {
        config_invalid = 1;
    }
    if (config_invalid) return_config = return_reset_leveled_config();
    return return_config;
}

struct leveled_config_struct return_reset_leveled_config(void)
{
    struct leveled_config_struct ret;
    ret.id[0] = 'L';
    ret.id[0] = 'C';
    ret.id[0] = 'F';
    ret.x = XMAX;
    ret.y = YMAX;
    ret.bpp = VIDEO_BITS;
    ret.flags = VIDEO_MODE;
    sprintf(ret.sli_last_file_opened, "\0");
    return ret;
}
